package com.pension.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pension.entity.ExcelData;
import com.pension.entity.Service;
import com.pension.service.ServiceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: ServiceController
 * @Description: GOOD
 * @author: ZZL
 * @date: 2021/2/1  13:56
 */
@Controller
@RequestMapping(value = "/service")
public class ServiceController {

    @Autowired
    ServiceService serviceService;

    @RequestMapping(value = "/order.do")
    public String songcan(Service service, HttpSession session){
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String realname = (String) session.getAttribute("realname");
        String place = (String) session.getAttribute("place");
        String s_lng = (String) session.getAttribute("s_lng");
        String s_lat = (String) session.getAttribute("s_lat");
        service.setRealname(realname);
        service.setS_address(place);
        service.setS_lat(s_lat);
        service.setS_lng(s_lng);
        serviceService.InsertService(service);
        return "right";
    }

    /**
     * @return serviceList
     */
    //--------------------------------------------------------------------------------------------------------
    @RequestMapping(value = "/list?pn=1")
    public String jumplist(){
        return "serviceList";
    }

    @RequestMapping(value = "/list")
    public String getEmps(@RequestParam(value = "pn",defaultValue = "1")
                                  Integer pn, Model model){
        //这不是一个分页查询
        //要实现分页查询引入pageheler分页查询
        Service service = new Service();
        //在查询前需要调用，传入页面，每页数据的大小
        PageHelper.startPage(pn,10);
        //startPage后面紧跟的查询是分页查询
        List<Service> services = serviceService.getAll();
        //使用pageInfo包装查询后的接口
        PageInfo page = new PageInfo(services,5);
        model.addAttribute("pageInfo",page);
        return "serviceList";
    }

//    @RequestMapping("/findByType")
//    public String findByName(
//            @RequestParam(defaultValue = "1") Integer typepn,
//            HttpServletRequest request, Model model) {
//
//        String type = request.getParameter("search");
//        System.out.println(type);
//        request.getServletContext().setAttribute("typedemo",type);
//        PageHelper.startPage(typepn, 10);
//        List<Service> typelist = serviceService.findByType(type);
//        PageInfo<Service> pageInfo = new PageInfo<Service>(typelist, 8);
//        model.addAttribute("typelist", pageInfo);
//        return "searchlist";
//    }
//--------------------------------------------------------------------------------------------------------
    @RequestMapping("/jumpserachlist")
    public String jumpserachlist(){
        return "searchlist";
    }

    //类型查询
    @RequestMapping("/findByType")
    public String findByName(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String type = (String) request.getServletContext().getAttribute("type");
        System.out.println(type);
        PageHelper.startPage(typepn, 10);
        List<Service> typelist = serviceService.findByType(type);
        PageInfo<Service> pageInfo = new PageInfo<Service>(typelist, 5);
        model.addAttribute("typelist", pageInfo);
        return "list/typelist";
    }
    //类型查询
    @RequestMapping("/findByType2")
    public String findByType2(  @RequestParam(defaultValue = "1") Integer typepn,
                                HttpServletRequest request, Model model){
        String type = request.getParameter("search");
        request.getServletContext().setAttribute("type",type);
        PageHelper.startPage(typepn, 10);
        List<Service> typelist = serviceService.findByType(type);
        PageInfo<Service> pageInfo = new PageInfo<Service>(typelist, 5);
        model.addAttribute("typelist", pageInfo);
        return "list/typelist";
    }
//--------------------------------------------------------------------------------------------------------
    //类型查询
    @RequestMapping("/findByName")
    public String findByRealName(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String name = (String) request.getServletContext().getAttribute("name");
        System.out.println(name);
        PageHelper.startPage(typepn, 10);
        List<Service> namelist = serviceService.findByName(name);
        PageInfo<Service> pageInfo = new PageInfo<Service>(namelist, 5);
        model.addAttribute("namelist", pageInfo);
        return "list/namelist";
    }
    //类型查询
    @RequestMapping("/findByName2")
    public String findByRealName2(  @RequestParam(defaultValue = "1") Integer typepn,
                                HttpServletRequest request, Model model){
        String name = request.getParameter("searchByName");
        System.out.println("获取到的值"+name);
        request.getServletContext().setAttribute("name",name);
        PageHelper.startPage(typepn, 10);
        List<Service> namelist = serviceService.findByName(name);
        PageInfo<Service> pageInfo = new PageInfo<Service>(namelist, 5);
        model.addAttribute("namelist", pageInfo);
        return "list/namelist";
    }

    //--------------------------------------------------------------------------------------------------------
    //类型查询
    @RequestMapping("/findByTime")
    public String findByTime(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String time = (String) request.getServletContext().getAttribute("time");
        System.out.println(time);
        PageHelper.startPage(typepn, 10);
        List<Service> timelist = serviceService.findByTime(time);
        PageInfo<Service> pageInfo = new PageInfo<Service>(timelist, 5);
        model.addAttribute("timelist", pageInfo);
        return "list/timelist";
    }
    //类型查询
    @RequestMapping("/findByTime2")
    public String findByTime2(  @RequestParam(defaultValue = "1") Integer typepn,
                                    HttpServletRequest request, Model model){
        String time = request.getParameter("searchByTime");
        System.out.println("获取到的值"+time);
        request.getServletContext().setAttribute("time",time);
        PageHelper.startPage(typepn, 10);
        List<Service> timelist = serviceService.findByTime(time);
        PageInfo<Service> pageInfo = new PageInfo<Service>(timelist, 5);
        model.addAttribute("timelist", pageInfo);
        return "list/timelist";
    }

    //--------------------------------------------------------------------------------------------------------
    //类型查询
    @RequestMapping("/findByDay")
    public String findByDay(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String day = (String) request.getServletContext().getAttribute("day");
        System.out.println(day);
        PageHelper.startPage(typepn, 10);
        List<Service> daylist = serviceService.findByDay(day);
        PageInfo<Service> pageInfo = new PageInfo<Service>(daylist, 5);
        model.addAttribute("daylist", pageInfo);
        return "list/daylist";
    }
    //类型查询
    @RequestMapping("/findByDay2")
    public String findByDay2(  @RequestParam(defaultValue = "1") Integer typepn,
                                HttpServletRequest request, Model model){
        String day = request.getParameter("searchByDay");
        System.out.println("获取到的值"+day);
        request.getServletContext().setAttribute("day",day);
        PageHelper.startPage(typepn, 10);
        List<Service> daylist = serviceService.findByDay(day);
        PageInfo<Service> pageInfo = new PageInfo<Service>(daylist, 5);
        model.addAttribute("daylist", pageInfo);
        return "list/daylist";
    }

    //--------------------------------------------------------------------------------------------------------
    //类型查询
    @RequestMapping("/findByClass")
    public String findByClass(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String sclass = (String) request.getServletContext().getAttribute("sclass");
        System.out.println(sclass);
        PageHelper.startPage(typepn, 10);
        List<Service> classlist = serviceService.findByClass(sclass);
        PageInfo<Service> pageInfo = new PageInfo<Service>(classlist, 5);
        model.addAttribute("classlist", pageInfo);
        return "list/classlist";
    }
    //类型查询
    @RequestMapping("/findByClass2")
    public String findByClass2(  @RequestParam(defaultValue = "1") Integer typepn,
                               HttpServletRequest request, Model model){
        String sclass = request.getParameter("searchByClass");
        System.out.println("获取到的值"+sclass);
        request.getServletContext().setAttribute("sclass",sclass);
        PageHelper.startPage(typepn, 10);
        List<Service> classlist = serviceService.findByClass(sclass);
        PageInfo<Service> pageInfo = new PageInfo<Service>(classlist, 5);
        model.addAttribute("classlist", pageInfo);
        return "list/classlist";
    }

    //--------------------------------------------------------------------------------------------------------
    //类型查询
    @RequestMapping("/findByPlace")
    public String findByPlace(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String place = (String) request.getServletContext().getAttribute("place");
        System.out.println(place);
        PageHelper.startPage(typepn, 10);
        List<Service> placelist = serviceService.findByPlace(place);
        PageInfo<Service> pageInfo = new PageInfo<Service>(placelist, 5);
        model.addAttribute("placelist", pageInfo);
        return "list/placelist";
    }
    //类型查询
    @RequestMapping("/findByPlace2")
    public String findByPlace2(  @RequestParam(defaultValue = "1") Integer typepn,
                                 HttpServletRequest request, Model model){
        String place = request.getParameter("searchByPlace");
        System.out.println("获取到的值"+place);
        request.getServletContext().setAttribute("place",place);
        PageHelper.startPage(typepn, 10);
        List<Service> placelist = serviceService.findByPlace(place);
        PageInfo<Service> pageInfo = new PageInfo<Service>(placelist, 5);
        model.addAttribute("placelist", pageInfo);
        return "list/placelist";
    }

    //--------------------------------------------------------------------------------------------------------
    //类型查询
    @RequestMapping("/findByAddress")
    public String findByAddress(
            @RequestParam(defaultValue = "1") Integer typepn,
            HttpServletRequest request, Model model) {
        String address = (String) request.getServletContext().getAttribute("address");
        System.out.println(address);
        PageHelper.startPage(typepn, 10);
        List<Service> addresslist = serviceService.findByAddress(address);
        PageInfo<Service> pageInfo = new PageInfo<Service>(addresslist, 5);
        model.addAttribute("addresslist", pageInfo);
        return "list/addresslist";
    }
    //类型查询
    @RequestMapping("/findByAddress2")
    public String findByAddress2(  @RequestParam(defaultValue = "1") Integer typepn,
                                 HttpServletRequest request, Model model){
        String address = request.getParameter("searchByAddress");
        System.out.println("获取到的值"+address);
        request.getServletContext().setAttribute("address",address);
        PageHelper.startPage(typepn, 10);
        List<Service> addresslist = serviceService.findByAddress(address);
        PageInfo<Service> pageInfo = new PageInfo<Service>(addresslist, 5);
        model.addAttribute("addresslist", pageInfo);
        return "list/addresslist";
    }

    //根据老人id查询老人订购的服务
    @RequestMapping("/findByOldManRealName")
    public String findByOldManRealName(@RequestParam(defaultValue = "1")Integer oldpn,Model model,HttpSession session){
        String realname = (String) session.getAttribute("realname");
        PageHelper.startPage(oldpn,10);
        List<Service> mylist = serviceService.findByOldManRealName(realname);
        PageInfo<Service> pageInfo = new PageInfo<Service>(mylist,5);
        model.addAttribute("mylist",pageInfo);
        return "mylist";
    }

    //删除用户
    String serviceUrl="redirect:/service/list?pn=1";
    @RequestMapping("serviceDeleteById")
    public ModelAndView deleteByid(@RequestParam("id")int id){
        serviceService.serviceDeleteById(id);
        return new ModelAndView(serviceUrl);
    }

    @GetMapping(value = "/daochuStatisticToExcel")
    @ResponseBody
    public Object daochuStatisticToExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            List list = serviceService.getAll();
            String fileName = new String("服务订单.xlsx".getBytes(), "ISO-8859-1");
            response.addHeader("Content-Disposition", "filename=" + fileName);
            ServletOutputStream out = response.getOutputStream();
            EasyExcelFactory.write(out, ExcelData.class).sheet("列表").doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    
}
